package com.sysd.mail.smtp.command;

import com.sysd.mail.config.ServerConfig;
import com.sysd.mail.pop.command.BasePopCommand;
import com.sysd.mail.smtp.SmtpSession;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * project: mail
 *
 * @author bsh
 * @description EhloCommand
 * @dataTime 2023/3/28 8:49
 * @company 丽水信阳软件科技有限公司
 */
public class EhloCommand extends BaseSmtpCommand {
    public EhloCommand() {
        super("EHLO");
    }

    /**
     * 执行命令
     *
     * @param command     命令名称
     * @param smtpSession smtp session
     * @return smtp session
     * @throws IOException io exception
     */
    @Override
    public SmtpSession execute(String command, SmtpSession smtpSession) throws IOException {
        String[] args = this.getCommandNameAndArgs(command);
        if (args.length < 2) {
            smtpSession.write("501 Syntax: EHLO hostname\r\n");
            return smtpSession;
        }
        smtpSession.resetMailTransaction();
        smtpSession.setHello(args[1]);
//		postfix returns...
//		250-server.host.name
//		250-PIPELINING
//		250-SIZE 10240000
//		250-ETRN
//		250 8BITMIME
        int maxSize = ServerConfig.MAX_MESSAGE_SIZE;
        String response = "250-" +
                ServerConfig.DOMAIN +
                "\r\n" + "250-8BITMIME" +
                "\r\n" + "250-SIZE " +
                maxSize +
                "\r\n" + "250-AUTH LOGIN PLAIN" +
                "\r\n" + "250 Ok\r\n";
        smtpSession.write(response);
        return smtpSession;
    }
}
